var orderHandle = function(){

    /**
     *在订单列表页面和订单详情页面，在status不同的情况下，按钮不同，
     *
     * */

//确认收货
    var confirmAccept = function(){
        $('.confirmAccept').on('click', function() {
            var orderId = $(this).attr('data-orderId');
            $('#acceptModal').modal({
                relatedTarget: this,
                onConfirm: function (options) {
                    $.post('/editOrder/' + orderId, {orderStatus: 3}).then(function (msg) {
                        if (msg.code == 200) {
                            messageBox("收货成功");
                            location.reload();
                        }else if(msg.code == 103){
                            messageBox("您还未登录，请登录后操作");
                            doLogin();
                        }else {
                            messageBox(msg.message,'alert');console.log('fail')
                        }
                    })
                }
            });
        })
    };


    //删除订单
    var deleteOrder = function(){
        $('.deleteOrder').on('click', function() {
            var orderId = $(this).attr('data-orderId');
            $('#deleteModal').modal({
                relatedTarget: this,
                onConfirm: function (options) {
                    $.post('/deleteOrder/' + orderId, {}).then(function (msg) {
                        if (msg.code == 200) {
                            messageBox("删除成功");
                            window.location.href="/orders";
                        }else if(msg.code == 103){
                            messageBox("您还未登录，请登录后操作");
                            doLogin();
                        } else {
                            messageBox(msg.message,'alert');
                        }
                    })
                }
            });
        })
    };

    //取消订单
    var orderCancel = function () {
        $('.cancelOrder').on('click', function() {
            console.log(0);
            var orderId = $(this).attr('data-orderId');
            $('#cancelModal').modal({
                relatedTarget: this,
                onConfirm: function (options) {
                    $.post('/editOrder/' + orderId, {orderStatus: 5}).then(function (msg) {
                        if (msg.code == 200) {
                            messageBox("交易已关闭");
                            location.reload();
                        }else if(msg.code == 103){
                            messageBox("您还未登录，请登录后操作");
                            doLogin();
                        } else {
                            messageBox(msg.message,'alert');console.log('fail')
                        }
                    })
                }
            });
        })
    };


    var payOrder = function(){
        $('.payOrder').on('click',function(){
            var orderId = $(this).attr('data-orderId');
            var payPrice = $(this).attr('data-price');
            if(getUserLoginStatus()){
                var info = new Base64().encode(JSON.stringify({
                    objectId: orderId,
                    payPrice: payPrice
                }));
                window.location.href="/payment?info="+info;
            }else{
                doLogin();
            }
        })
    };

    return {
        init:function(){
            confirmAccept();
            deleteOrder();
            orderCancel();
            payOrder();
        }
    }
}();
